function getStation() {
    getLocation();
}

function sendData(crd, customStation) {
    var newStation = {
        "coordinates": crd,
        "name": customStation
    };
    //Debugging
    console.log(crd, customStation);

    $.getJSON("http://f1r3wall.dyndns.org:3000/addNewStation?callback=?", newStation);
    window.setTimeout('location.reload()', 3000);
}

function addCb(msg) {
    console.log(msg);
}

function setStation() {
    var customStation2=document.getElementById("nameByLoc2").value;
    var Latitude=document.getElementById("Latitude").value;
    var Longitude=document.getElementById("Longitude").value;

    var successfulLocation2 = "<p> Your current position is: <br />";
    successfulLocation2 += 'Latitude : ' + Latitude + "<br />";
    successfulLocation2 += 'Longitude: ' + Longitude + "<br />";
    if (check_lat_lon(Latitude, Longitude)){
        createOutput("<p> Station added: " + customStation2 + "</p>");
        createOutput(successfulLocation2);
    } else {
        document.getElementById("savePar").innerHTML = "Coordinates syntax is incorrect!";
        return;
    }
    
    var crd = {
        latitude: Latitude,
        longitude: Longitude
    };
    sendData(crd,customStation2);
}

var options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
};

function success(pos) 
{
	try
	{
		var crd = pos.coords;
		var customStation = document.getElementById("nameByLoc").value;
		var successfulLocation = "<p> Your current position is: <br />";
		successfulLocation += 'Latitude : ' + crd.latitude + "<br />";
		successfulLocation += 'Longitude: ' + crd.longitude + "<br />";
		successfulLocation += 'More or less ' + crd.accuracy + ' meters. </p>';
		sendData(crd, customStation);
		clearPopup()
		createOutput("<p> Station added: " + customStation + "</p>");
		createOutput(successfulLocation);
	} 
	catch(err)
	{
		
	}
};

function error(err) {
    console.warn('ERROR(' + err.code + '): ' + err.message);
};

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error, options);


    } else {
        alert("Geolocation is not supported by this browser.");
    }
}

function createOutput(inputText) {
    document.getElementById("savePar").innerHTML+= "<br />" + inputText;
}

function check_lat_lon(lat, lon){
    var ck_lat = /^(-?[1-8]?\d(?:\.\d{1,18})?|90(?:\.0{1,18})?)$/;
    var ck_lon = /^(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,18})?|180(?:\.0{1,18})?)$/;

    var validLat = ck_lat.test(lat);
    var validLon = ck_lon.test(lon);
    if(validLat && validLon) {
        return true;
    } else {
        return false;
    }
}

function clearPopup() {
    document.getElementById("savePar").innerHTML = "";
}

function loadContent() {
    $.getJSON("http://f1r3wall.dyndns.org:3000/loadContent?callback=?");
    
}

// callback jsonp
function loadContent2(content) {
    var i;
    for (i=0; i < content.length; i++)
    {
        $('#manuallyAddedTable').append($('<li>').append(content[i].name).append(
            $('<span>').data("id", content[i]).attr('class', 'ui-li-count del-station').append("Delete")
        ));
    }
    $('#manuallyAddedTable').listview('refresh');
}

$(document).on("#favoritesStationList", "pageinit", function( event, ui ) {
    loadContent();
});

$('.del-station').live("click", function(e){
    var target = e.target;
    var id = $(target).data('id')._id;
    console.log(id);
    $.getJSON("http://f1r3wall.dyndns.org:3000/delStation?callback=?&id="+id);
});

function delStationCb(id) {
    console.log("del callback");
    window.setTimeout('location.reload()', 100);
}